package com.sailorj.controller;

import com.sailorj.entity.TbWork;
import com.sailorj.service.TbWorkService;
import com.sailorj.util.NumUtil;
import com.sailorj.util.PageBean;
import com.sailorj.util.SysLoging;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @Description: 工作日志管理
* @Param:
* @return:
* @Author: huangshengjun
* @Date: 2020/4/19 19:29
*/
@CrossOrigin  //后端解决跨域方法注解
@Controller
@RequestMapping("/work") //访问的路径
public class TbWorkController {
    private static final Logger logger = LoggerFactory.getLogger(TbWorkController.class);
    @Autowired
    private TbWorkService tbWorkService;

    @ResponseBody
    @SysLoging("添加工作日志信息") //根据SysLoging的注解信息，进行方法的注释
    @RequestMapping(value = "/addWork",method = RequestMethod.POST) //restful风格
    public String addWork(TbWork Work){
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
        Work.setCreatedTime(formatter.format(date));
        Work.setWorkId(NumUtil.getNum(7));
        Work.setIsDelete(0);
        tbWorkService.addWork(Work);
        return "";
    }

    @ResponseBody
    @SysLoging("根据id查询工作日志信息")
    @RequestMapping(value = "/getWorkById",method = RequestMethod.GET)
    public TbWork getWorkById(int id){
        return tbWorkService.getWorkById(id);
    }

    @ResponseBody
    @SysLoging("获取工作日志信息列表")
    @RequestMapping(value = "/getWorkList",method = RequestMethod.GET)
    PageBean getTbWorkList(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer size){
        return tbWorkService.getWorkList(page,size);
    }

    @ResponseBody
    @SysLoging("根据时间获取工作日志")
    @RequestMapping(value = "/getWorkListByTime",method = RequestMethod.GET)
    PageBean getWorkListByTime(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer size,String time){
        logger.info("查询的时间"+time);
        return tbWorkService.getWorkListByTime(page,size,time);
    }

    @ResponseBody
    @SysLoging("删除工作日志信息")
    @RequestMapping(value = "/deleteWork",method = RequestMethod.DELETE)
    public String deleteTbWork(TbWork Work) {
        TbWork Work1 = tbWorkService.getWorkById(Work.getId());
        Work1.setIsDelete(1);
        tbWorkService.updateWorkById(Work1);
        return "";
    }

    @ResponseBody
    @SysLoging("更新工作日志信息")
    @RequestMapping(value = "/updateWork",method = RequestMethod.PUT)
    public String updateWork(TbWork Work) {
        TbWork Work1 = tbWorkService.getWorkById(Work.getId());
        Work.setCreatedTime(Work1.getCreatedTime());
        Work.setIsDelete(0);
        tbWorkService.updateWorkById(Work);
        return "";
    }
}
